//
//  NSMutableDictionary+KLException.m
//  KoalaUtilKit
//
//  Created by Tommy on 15/1/29.
//  Copyright (c) 2015年 Koala Team. All rights reserved.
//

#import "NSMutableDictionary+KLException.h"
#import "KLLog.h"
#import "NSObject+KLSwizzle.h"

@implementation NSMutableDictionary (KLException)

#if KLENABLE_EXCEPTION_SWIZZLE
+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class cls = [[self dictionaryWithCapacity:0] class];
        [cls kl_swizzleInstanceSelector:@selector(setObject:forKey:) withNewSelector:KLHookedSel(setObject:forKey:)];
    });
}

- (void)KLHookedMethod(setObject:(id)object forKey:(id <NSCopying>)key){
    if(object){
        @try{
            [self KLHookedMethod(setObject:object forKey:key)];
        }
        @catch (NSException *exception) {
            NSAssert(0,@"exception:%@",exception);
            KLLogError(@"exception:%@",exception);
        }
    }else{
        NSAssert(0,@"can't setObject nil forKey:%@  to %@",key,self);
        KLLogError(@"can't setObject nil forKey:%@  to %@",key,self);
    }
}

#endif //KLENABLE_EXCEPTION_SWIZZLE

@end
